package cn.how2go.net.server.service;


import cn.how2go.net.server.base.LockList;
import cn.how2go.net.server.base.SessionData;
import cn.how2go.net.server.bean.ByteMessage;

/**
 * Created by Administrator on 2019/3/7.
 */
public abstract class ByteMessageService {
    public ByteMessage read(){
        ByteMessage bytes = null;
        try{
            synchronized (LockList.messageLock){
                if(SessionData.BYTEMESSAGE.size()<=0){
                    LockList.messageLock.wait();
                }
                bytes = SessionData.BYTEMESSAGE.poll();
                callback(bytes);
                LockList.messageLock.notify();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return bytes;
    }

    public abstract void callback(ByteMessage bytes);

    /**
     * 向报文队列写入报文
     * @param bytes
     */
    public static void write(ByteMessage bytes){
        try{
            synchronized (LockList.messageLock){
                if(SessionData.BYTEMESSAGE.size()>0){
                    LockList.messageLock.wait();
                }
                SessionData.BYTEMESSAGE.offer(bytes);
                LockList.messageLock.notify();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
